/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-02-03 17:25:45
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 15:42:48
 */

'use strict';

define([
    '../../../modules/common/service'
], function(
    service
) {
    function me() {
        var self = this;
        self.kit = kit;
        self._ = _;

        /**
         * 简单封装 post for _p
         * @private
         * @method _p
         * @param {Object} url
         * @param {Object} params
         * @param {Object} callback
         * @param {Object} errorCallback
         * @return {Object} description
         */
        this.p = function(url, params, callback, errorCallback) {
            if (!params) {
                params = {}
            }
            if (params._disabledShowloading === true) {
                console.info('不显示loading');
            } else {
                kit.loading.show($('.fmp-body'));
            }

            service.work(url, params, 'POST', errorCallback).then(function(result) {
                self._successCallback(result, callback, errorCallback);
            }).done(function() {
                kit.loading.hide($('.fmp-body'));
            }).fail(function() {
                kit.loading.hide($('.fmp-body'));
                if (errorCallback) {
                    errorCallback();
                    return;
                }
                showAlert({
                    content: '网络出现错误!'
                })
            });
        }


        /**
         * 简单封装 post for _p  不加入loading
         * @private
         * @method _p
         * @param {Object} url
         * @param {Object} params
         * @param {Object} callback
         * @param {Object} errorCallback
         * @return {Object} description
         */
        this.post = function(url, params, callback, errorCallback) {
            if (!params) {
                params = {}
            }
            service.work(url, params, 'POST', errorCallback).then(function(result) {
                self._successCallback(result, callback, errorCallback);
            }).done(function() {
                kit.loading.hide($('.fmp-body'));
            }).fail(function() {
                if (errorCallback) {
                    errorCallback();
                    return;
                }
                showAlert({
                    content: '网络出现错误!'
                })
            });
        }


        /**
         * 简单封装 post for _p
         * @private
         * @method _p
         * @param {Object} url
         * @param {Object} params
         * @param {Object} callback
         * @param {Object} errorCallback
         * @return {Object} description
         */
        this.g = function(url, params, callback, errorCallback) {
            kit.loading.show($('.fmp-body'));
            if (!params) {
                params = {}
            }
            service.work(url, params, 'GET', errorCallback).then(function(result) {
                self._successCallback(result, callback, errorCallback);
            }).done(function() {
                kit.loading.hide($('.fmp-body'));
            }).fail(function() {
                kit.loading.hide($('.fmp-body'));
                showAlert({
                    content: '网络出现错误!'
                })
            });
        }



        /**
         *
         * @method _get
         * @description [对 get 请求的封装.]
         * @param  {string}   url      [请求的 url]
         * @param  {object}   params   [请求的参加]
         * @param  {Function} callback [回调函数]
         * @return {[type]}            [description]
         */
        this._get = function(url, params, callback, errorCallback) {
            return this._ajax(url, 'get', params, callback, errorCallback);
        }

        /**
         *
         * @method _post
         * @description [对 post 请求的封装]
         * @param  {string}   url      [请求的url]
         * @param  {object}   params   [请求的参加]
         * @param  {Function} callback [回调函数]
         * @return {[type]}            [description]
         */
        this._post = function(url, params, callback, errorCallback) {
            return this._ajax(url, 'post', params, callback, errorCallback);
        }


        /**
         *
         * @method _ajax
         * @description [jquery ajax 请求的封装, 私有函数.]
         * @private
         * @param  {string}   url      [请求的url]
         * @param  {object}   params   [请求的参加]
         * @param  {Function} callback [回调函数]
         * @return {[type]}            [description]
         */
        this._ajax = function(url, type, params, callback, errorCallback) {
            var self = this;
            var req = null;
            if (_.isNull(params)) {
                params = {};
            }
            kit.loading.show($('.fmp-body'));
            var href = cors_config.api_host + url;
            req = $.ajax({
                url: href,
                type: type,
                data: params,
                xhrFields: {
                    withCredentials: true
                },
                success: function(result) {
                    self._successCallback(result, callback, errorCallback);
                },
                complete: function(xhr, textStatus) {
                    kit.loading.hide($('.fmp-body'));
                },
                error: function(xhr, textStatus, errorThrown) {
                    kit.loading.hide($('.fmp-body'));
                    showAlert({
                        content: '网络出现错误!'
                    })
                }
            });
            return req;
        }


        /**
         * { 返回结果后的的处理 }
         *
         * @method     _successCallback
         * @param      {<type>}    result         The result
         * @param      {Function}  callback       The callback
         * @param      {Function}  errorCallback  The error callback
         */
        this._successCallback = function(result, callback, errorCallback) {
            if (result.flag == 'success') {
                if (callback) {
                    callback(result.data);
                }
            } else if (result.flag == 'fail') {
                var msg = result.msg || result.errorMsg||'error';
                if (result.errorCode) {
                    kit.alert(result.errorCode, msg);
                } else {
                    kit.alert2(msg);
                }
                if (errorCallback) {
                    errorCallback(result)
                }
            } else if (_.isString(result)) {
                if (callback) {
                    try {
                        var jsonobj = JSON.parse(result);
                        if (jsonobj.errorCode == '60121' || jsonobj.errorCode == '60101') {
                            window.location.hash.indexOf('account') != -1 ? '' : window.location.hash = '#!account';
                        } else {
                            callback(jsonobj);
                        }
                    } catch (e) {
                        console.error(e);
                        callback(result);
                    };
                }
            } else {
                callback(result);
            }
        }

    };
    return me;
});
